//
//  YWWebViewController.h
//  maintain
//
//  Created by 郑想 on 15/10/19.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "NJKWebViewProgressView.h"
#import "UIColor+CustomColors.h"
#import "YWWebViewController.h"

@interface YWWebViewController () {
    UIWebView* newsWebView;
    NJKWebViewProgressView* _progressView;
    NJKWebViewProgress* _progressProxy;
}

@end

@implementation YWWebViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    newsWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
    [newsWebView setBackgroundColor:[UIColor customBackgroudColor]];
    [newsWebView setOpaque:NO];
    newsWebView.suppressesIncrementalRendering = YES;
    newsWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    _progressProxy = [[NJKWebViewProgress alloc] init];
    newsWebView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    [self.view addSubview:newsWebView];

    CGFloat progressBarHeight = 2.f;
    CGRect navigaitonBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigaitonBarBounds.size.height - progressBarHeight, navigaitonBarBounds.size.width, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

    NSURLRequest* req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_passURL]];
    [newsWebView loadRequest:req];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:_progressView];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // Remove progress view
    // because UINavigationBar is shared with other ViewControllers
    [_progressView removeFromSuperview];
}

#pragma mark - NJKWebViewProgressDelegate
- (void)webViewProgress:(NJKWebViewProgress*)webViewProgress updateProgress:(float)progress
{
    [_progressView setProgress:progress animated:YES];
    self.title = [newsWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
@end
